package com.ktgj.pay.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.PascalNameFilter;
import com.ktgj.pay.domain.InvoiceFpxz;
import com.ktgj.pay.domain.InvoiceInterFace;
import com.ktgj.pay.domain.MoInterfaceResponse;
import com.ktgj.pay.domain.MoSendMail;
import com.ktgj.pay.utils.RegexUtil;
import com.ktgj.pay.service.InvoiceInterFaceService;
import com.ktgj.pay.utils.HttpHelper;
import com.ktgj.pay.utils.ResponseModel;
import com.ktgj.pay.utils.SystemConstants;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

/**
 * 电子发票-第三方接口对接，请求控制器
 * @author Maming
 *
 */
@Api(tags = "invoicexs")
@ControllerAdvice
@Controller
@RequestMapping("/invoicex")
public class InvoiceInterFaceController {
	
	@Autowired
	InvoiceInterFaceService invoiceService;
	
	/**
	 * 电子发票：发票开具接口
	 * @param face
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/fpkj")
	public ResponseModel invoiceFPKJ(@RequestBody InvoiceInterFace face){
		ResponseModel responseModel = new ResponseModel();
		String Json= JSON.toJSONString(face);
		//测试Json
//		Json = "{\"swno\": \"10002029\","
//				+ "\"sellerTaxNo\": \"91110105599660274U\","
//				+ "\"custName\": \"同方节能（友谊）热力有限公司\","
//				+ "\"custTaxNo\": \"912305220777877370\","
//				+ "\"custAddrPhone\": \"\","
//				+ "\"custBankAccount\": \"\","
//				+ "\"custTelephone\": \"\","
//				+ "\"custEmail\": \"\","
//				+ "\"invMemo\": \"2020-01-09 06:20 CA1691 北京-佳木斯 姜德民\","
//				+ "\"invType\": \"51\","
//				+ "\"orderDate\": \"2020-01-09 08:56:23\","
//				+ "\"orderType\": \"1\","
//				+ "\"drawer\": \"景栗阳\","
//				+ "\"reviewer\": \"于海双\","
//				+ "\"payee\": \"韩云\","
//				+ "\"origInvcode\": \"\","
//				+ "\"origInvno\": \"\","
//				+ "\"redCode\": \"\","
//				+ "\"sellerAddrPhone\": \"北京市朝阳区来广营5号2E 010-84926666\","
//				+ "\"sellerBankAccount\": \"交通银行北京媒体村支行 110061353018010052603\","
//				+ "\"machineCode\": \"\","
//				+ "\"oilIdentification\": \"0\","
//				+ "\"kpType\": \"0\","
//				+ "\"items\": ["
//				+ "{\"goodsName\": \"机票款\","
//				+ "\"lineType\": \"0\","
//				+ "\"lineCode\": \"1\","
//				+ "\"model\": \"\","
//				+ "\"unit\": \"\","
//				+ "\"qty\": \"\","
//				+ "\"price\": \"\","
//				+ "\"taxPrice\": \"\","
//				+ "\"amount\": \"\","
//				+ "\"taxAmount\": \"1257.00\","
//				+ "\"tax\": \"\","
//				+ "\"taxRate\": \"0.06\","
//				+ "\"taxCode\": \"3070301000000000000\","
//				+ "\"taxPre\": \"0\","
//				+ "\"taxPrecon\": \"\","
//				+ "\"zeroTax\": \"\","
//				+ "\"gropGoodsNo\": \"\","
//				+ "\"taxDeduction\": \"\","
//				+ "\"taxVersion\": \"30.0\"}]}";
		/*
		 * 调用Service 发票开具 业务处理
		 */
		MoInterfaceResponse response = invoiceService.invoiceFPKJ(face.getOrderId(), Json);
		if(response.isSign()){
			responseModel = SuccessDo(responseModel,response);
		}else{
			responseModel = FailDo(responseModel,response);
		}
		return responseModel;
	}

	/**
	 * 电子发票-发票下载
	 * @param request
	 * @param response
	 * @param invoiceFpxz
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/fpxz")
	public ResponseModel invoiceFPXZ(HttpServletRequest request, HttpServletResponse response, @RequestBody InvoiceFpxz invoiceFpxz){
		ResponseModel responseModel = new ResponseModel();
		//String path = request.getSession().getServletContext().getRealPath(SystemConstants.INVOICE_PDF_URL);
		String path = "D:/Invoice" + SystemConstants.INVOICE_PDF_URL;
		//测试Json
		//Json = "{\"swno\": \"1485551688386347008\",\"sellerTaxNo\": \"91110105599660274U\"}";
		/*
		 * 调用Service 发票开具 业务处理
		 */
		String Json= JSON.toJSONString(invoiceFpxz);
		MoInterfaceResponse IFresponse = invoiceService.invoiceFPXZ(Json, path);
		if(IFresponse.isSign()){
			responseModel = SuccessDo(responseModel,IFresponse);
		}else{
			responseModel = FailDo(responseModel,IFresponse);
		}
		return responseModel;
	}

	/**
	 * 电子发票-发票冲红
	 * @param Json
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/fpch")
	public ResponseModel invoiceFPCH(String Json){
		ResponseModel responseModel = new ResponseModel();
		//测试Json
//		Json = "{\"swno\": \"CH10000094\",\"sellerTaxNo\": \"91110105599660274U\",\"machineCode\": \"\",\"kpType\": \"0\",\"origInvcode\": \"011001900511\",\"origInvno\": \"54940010\"}";
		MoInterfaceResponse response = invoiceService.invoiceFPCH(Json);
		if(response.isSign()){
			responseModel = SuccessDo(responseModel,response);
		}else{
			responseModel = FailDo(responseModel,response);
		}
		return responseModel;
	}

	/**
	 * 电子发票-发票余量查询
	 * @param Json
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/fpylcx")
	public ResponseModel invoiceFPYLCX(String Json){
		ResponseModel responseModel = new ResponseModel();
		//测试Json
//		Json = "{\"sellerTaxNo\": \"91110105599660274U\",\"machineCode\": \"\",\"invType\": \"51\"}";
		MoInterfaceResponse response = invoiceService.invoiceFPYLCX(Json);
		if(response.isSign()){
			responseModel = SuccessDo(responseModel,response);
		}else{
			responseModel = FailDo(responseModel,response);
		}
		return responseModel;
	}

	/**
	 * 电子发票-订单删除
	 * @param Json
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/ddsc")
	public ResponseModel invoiceDDSC(String Json){
		ResponseModel responseModel = new ResponseModel();
		//测试Json
//		Json = "{\"swno\": \"10000013\",\"sellerTaxNo\": \"91110105599660274U\"}";
		MoInterfaceResponse response = invoiceService.invoiceDDSC(Json);
		if(response.isSign()){
			responseModel = SuccessDo(responseModel,response);
		}else{
			responseModel = FailDo(responseModel,response);
		}
		return responseModel;
	}

	/**
	 * 发送邮件
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
    @RequestMapping("/sendmail")
    public ResponseModel sendMail(HttpServletRequest request, HttpServletResponse response) {
	//    	 Json = "{\"isContainFJ\": \"true\",\"fileName\": \"20191011_011001900411_14825673.pdf\",\"toAddress\": \"1187514434@qq.com\",\"title\": \"测试发票邮件\",\"context\": \"邮件正文\"}";
		ResponseModel responseModel = new ResponseModel();
		String Json = HttpHelper.getBodyString(request);
		JSONObject JSR = JSONObject.parseObject(Json);
		if(!RegexUtil.isEmail(JSR.getString("toAddress"))){
			responseModel.setSuccess(false);
			responseModel.setMessage("邮箱地址不正确，请重新填写。");
			return responseModel;
		}
		//String path = request.getSession().getServletContext().getRealPath(SystemConstants.INVOICE_PDF_URL);
		String path = "G:/Invoice" + SystemConstants.INVOICE_PDF_URL;
		MoSendMail result = invoiceService.sendStoreAlertMail(Json, path);
		if(result.isSend()){
			responseModel.setSuccess(true);
			responseModel.setMessage("邮件发送成功");
			responseModel.setData(result);
		}else{
			responseModel.setSuccess(false);
			responseModel.setMessage("邮件发送失败");
			responseModel.setData(result);
		}
		return responseModel;
    }
	
	
	/**
	 * 成功返回模板
	 * @param model
	 * @param response
	 * @return
	 */
	public ResponseModel SuccessDo(ResponseModel model,MoInterfaceResponse response){
		model.setSuccess(true);
		model.setMessage("请求接口成功");
		model.setData(response);
		return model;
	}
	/**
	 * 失败返回模板
	 * @param model
	 * @param response
	 * @return
	 */
	public ResponseModel FailDo(ResponseModel model,MoInterfaceResponse response){
		model.setSuccess(false);
		model.setMessage("请求接口失败");
		model.setData(response);
		return model;
	}

}
